package com.winexpress.sr;

import com.winexpress.product.Category;
import com.winexpress.product.ProductManagementWS;
import com.winexpress.product.ProductManagementWS_Service;
import com.winexpress.product.Wine;
import com.winexpress.util.SessionAttributes;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

@WebServlet(name = "SalesReportManagement", urlPatterns = {"/salesReportManagement/*"})
public class SalesReportManagement extends HttpServlet {

    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/WineXpress-war/SalesReportManagementWS.wsdl")
    private SalesReportManagementWS_Service service;
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/WineXpress-war/ProductManagementWS.wsdl")
    private ProductManagementWS_Service service1;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {

            RequestDispatcher dispatcher;
            ServletContext servletContext = getServletContext();

            String page = request.getPathInfo();
            page = page.substring(1);
            System.out.println(page);
              HttpSession session = request.getSession();
               if (session.getAttribute(SessionAttributes.USER_ID) == null || session.getAttribute(SessionAttributes.IS_ADMIN) == null ||session.getAttribute(SessionAttributes.IS_ADMIN).toString().equals("false")) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
                 System.out.println(session.getAttribute(SessionAttributes.IS_ADMIN));
                return;
            }        

            if ("salesReportSubmit".equals(page) || "profitReportSubmit".equals(page)) {
               
                com.winexpress.sr.SalesReportManagementWS port = service.getSalesReportManagementWSPort();
                List<SalesReport> temp = port.listSalesYear();
                // System.out.println(listtemp.get(1).getSales());
                SalesReport[] list = new SalesReport[temp.size()];
                for (int i = 0; i < temp.size(); i++) {
                    list[i] = temp.get(i);
                }
                request.setAttribute("salesList", list);

                request.setAttribute("y0", list[0].getYr());
                request.setAttribute("y1", list[1].getYr());
                request.setAttribute("y2", list[2].getYr());

                request.setAttribute("y0s", list[0].getSales());
                request.setAttribute("y1s", list[1].getSales());
                request.setAttribute("y2s", list[2].getSales());

                request.setAttribute("y0p", list[0].getProfit());
                request.setAttribute("y1p", list[1].getProfit());
                request.setAttribute("y2p", list[2].getProfit());

            } else if ("salesYear".equals(page) || "profitYear".equals(page)) {
                com.winexpress.sr.SalesReportManagementWS port = service.getSalesReportManagementWSPort();
                String selectedYr = request.getParameter("selectedYear");
                // System.out.println(selectedYr);
                ReportObject obj = new ReportObject();
                obj.setSingleYear(selectedYr);
                List<SalesReport> temp = port.salesByYear(obj);
                SalesReport[] list = new SalesReport[temp.size()];
                for (int i = 0; i < temp.size(); i++) {
                    list[i] = temp.get(i);
                }
                request.setAttribute("salesByYear", list);
                request.setAttribute("year", selectedYr);
                request.setAttribute("m1s", list[0].getSales());
                request.setAttribute("m2s", list[1].getSales());
                request.setAttribute("m3s", list[2].getSales());
                request.setAttribute("m4s", list[3].getSales());
                request.setAttribute("m5s", list[4].getSales());
                request.setAttribute("m6s", list[5].getSales());
                request.setAttribute("m7s", list[6].getSales());
                request.setAttribute("m8s", list[7].getSales());
                request.setAttribute("m9s", list[8].getSales());
                request.setAttribute("m10s", list[9].getSales());
                request.setAttribute("m11s", list[10].getSales());
                request.setAttribute("m12s", list[11].getSales());

                request.setAttribute("m1p", list[0].getProfit());
                request.setAttribute("m2p", list[1].getProfit());
                request.setAttribute("m3p", list[2].getProfit());
                request.setAttribute("m4p", list[3].getProfit());
                request.setAttribute("m5p", list[4].getProfit());
                request.setAttribute("m6p", list[5].getProfit());
                request.setAttribute("m7p", list[6].getProfit());
                request.setAttribute("m8p", list[7].getProfit());
                request.setAttribute("m9p", list[8].getProfit());
                request.setAttribute("m10p", list[9].getProfit());
                request.setAttribute("m11p", list[10].getProfit());
                request.setAttribute("m12p", list[11].getProfit());

            } else if ("salesPeriod".equals(page)) {
                com.winexpress.sr.SalesReportManagementWS port = service.getSalesReportManagementWSPort();
                String fromYear = request.getParameter("fromYear");
                String fromMonth = request.getParameter("fromMonth");
                String fromDay = request.getParameter("fromDay");
                String toYear = request.getParameter("toYear");
                String toMonth = request.getParameter("toMonth");
                String toDay = request.getParameter("toDay");
                ReportObject obj = new ReportObject();
                obj.setFromYr(fromYear);
                obj.setFromMth(fromMonth);
                obj.setFromDay(fromDay);
                obj.setToYr(toYear);
                obj.setToMth(toMonth);
                obj.setToDay(toDay);
                SalesReport salesreport = port.salesByPeriod(obj);
                double sales = salesreport.getSales();
                double profit = salesreport.getProfit();

                double[] wineSales = new double[200];
                double[] wineProfit = new double[200];
                int[] qty = new int[200];
                for (int i = 0; i < 200; i++) {
                    wineSales[i] = salesreport.getWineSales().get(i);
                    wineProfit[i] = salesreport.getWineProfit().get(i);
                    qty[i] = salesreport.getQty().get(i);
                }

//                System.out.println(sales);
//                System.out.println(profit);
                request.setAttribute("fromYear", fromYear);
                request.setAttribute("fromMonth", fromMonth);
                request.setAttribute("fromDay", fromDay);
                request.setAttribute("toYear", toYear);
                request.setAttribute("toMonth", toMonth);
                request.setAttribute("toDay", toDay);

                request.setAttribute("sales", sales);
                request.setAttribute("profit", profit);
                request.setAttribute("winesales", wineSales);
                request.setAttribute("wineprofit", wineProfit);
                request.setAttribute("wineqty", qty);
                request.setAttribute("report", salesreport);
            } else if ("selectProductPeriod".equals(page)) {
                ProductManagementWS port = service1.getProductManagementWSPort();
                List<Wine> winelist = port.listWines();
                request.setAttribute("wineList", winelist);
            } else if ("salesPeriodProduct".equals(page)) {
                com.winexpress.sr.SalesReportManagementWS port = service.getSalesReportManagementWSPort();
                String fromYear = request.getParameter("fromYear");
                String fromMonth = request.getParameter("fromMonth");
                String fromDay = request.getParameter("fromDay");
                String toYear = request.getParameter("toYear");
                String toMonth = request.getParameter("toMonth");
                String toDay = request.getParameter("toDay");
                String wineid = request.getParameter("wineid");
                ReportObject obj = new ReportObject();
                obj.setFromYr(fromYear);
                obj.setFromMth(fromMonth);
                obj.setFromDay(fromDay);
                obj.setToYr(toYear);
                obj.setToMth(toMonth);
                obj.setToDay(toDay);
                obj.setWineid(wineid);
                SalesReport salesreport = port.salesByPeriodProduct(obj);
                double sales = salesreport.getSales();
                double profit = salesreport.getProfit();
                int qty = salesreport.getSingleQty();

//                System.out.println(sales);
//                System.out.println(profit);
                request.setAttribute("fromYear", fromYear);
                request.setAttribute("fromMonth", fromMonth);
                request.setAttribute("fromDay", fromDay);
                request.setAttribute("toYear", toYear);
                request.setAttribute("toMonth", toMonth);
                request.setAttribute("toDay", toDay);

                request.setAttribute("sales", sales);
                request.setAttribute("profit", profit);
                request.setAttribute("wineqty", qty);

                request.setAttribute("wineid", wineid);
            } else if ("selectCategoryPeriod".equals(page)) {
                ProductManagementWS port = service1.getProductManagementWSPort();
                List<Category> catlist = port.listCategory();
                request.setAttribute("catList", catlist);
            } else if ("salesPeriodCategory".equals(page)) {
                com.winexpress.sr.SalesReportManagementWS port = service.getSalesReportManagementWSPort();
                String fromYear = request.getParameter("fromYear");
                String fromMonth = request.getParameter("fromMonth");
                String fromDay = request.getParameter("fromDay");
                String toYear = request.getParameter("toYear");
                String toMonth = request.getParameter("toMonth");
                String toDay = request.getParameter("toDay");
                String catid = request.getParameter("catid");

                ReportObject obj = new ReportObject();
                obj.setFromYr(fromYear);
                obj.setFromMth(fromMonth);
                obj.setFromDay(fromDay);
                obj.setToYr(toYear);
                obj.setToMth(toMonth);
                obj.setToDay(toDay);
                obj.setCatid(catid);

                SalesReport salesreport = port.salesByPeriodCategory(obj);
                double sales = salesreport.getSales();
                double profit = salesreport.getProfit();
                int qty = salesreport.getSingleQty();

//                System.out.println(sales);
//                System.out.println(profit);
                request.setAttribute("fromYear", fromYear);
                request.setAttribute("fromMonth", fromMonth);
                request.setAttribute("fromDay", fromDay);
                request.setAttribute("toYear", toYear);
                request.setAttribute("toMonth", toMonth);
                request.setAttribute("toDay", toDay);

                request.setAttribute("sales", sales);
                request.setAttribute("profit", profit);
                request.setAttribute("catqty", qty);

                request.setAttribute("catid", catid);
            } else if ("awis".equals(page)) {
                com.winexpress.sr.SalesReportManagementWS port = service.getSalesReportManagementWSPort();
                SalesReport report = port.awis();
                String title = report.getTitle();
                String MedianLoadTime = report.getMedianLoadTime();
                String Percentile = report.getPercentile();
                String Rank = report.getRank();
                String RankByCountry = report.getRankByCountry();
                String UsageStatistics = report.getUsageStatistics();
                String ContributingSubdomains = report.getContributingSubdomains();
                request.setAttribute("Title", title);
                request.setAttribute("MedianLoadTime", MedianLoadTime);
                request.setAttribute("Percentile", Percentile);
                request.setAttribute("Rank", Rank);
                request.setAttribute("RankByCountry", RankByCountry);
                request.setAttribute("UsageStatistics", UsageStatistics);
                request.setAttribute("ContributingSubdomains", ContributingSubdomains);
            }

            dispatcher = servletContext.getNamedDispatcher(page);
            if (dispatcher == null) {
                dispatcher = servletContext.getNamedDispatcher("Error");
            }
            dispatcher.forward(request, response);
        } catch (Exception ex) {
            log("Exception in SalesReportManagement servlet");
            log(ex.toString());
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
